//
//  UUSupplierRegisterInfoVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 16/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUSupplierRegisterInfoVC.h"
#import <UUFoundation/UUFileManager.h>
#import <UUFoundation/UUCityManager.h>
#import <UUFoundation/DKProgressHUD.h>
#import "UUHybridManager+CharterSupplier.h"
#import "UUSupplierManager.h"
#import "UUUserManager.h"
#import "UULoginManager.h"
#import "UUPermissionManager.h"
#import "UUSupplierReviewStatusTipsView.h"
#import "UUSupplierRegisterVC.h"

@interface UUSupplierRegisterInfoVC ()

@property (nonatomic, strong) UUSupplierRegisterInfo *registerInfo;
@property (nonatomic, strong) UUSupplierReviewStatusTipsView *reviewStatusTipsView;

@end

@implementation UUSupplierRegisterInfoVC

- (void)viewDidLoad {
    self.editable = NO;
    
    [super viewDidLoad];
    
    self.reviewStatusTipsView = [[UUSupplierReviewStatusTipsView alloc] initWithContainerViewController:self];
    
    self.webView.hidden = YES;
    
    [self reloadSupplierInfo];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self.reviewStatusTipsView update];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)fullName {
    return @"公司信息";
}

- (NSURLRequest *)detailRequest {
    return GetHybridManager().registerDetailURLRequest;
}

- (NSDictionary *)previewFields {
    if (self.registerInfo == nil) {
        return nil;
    } else {
        return @{
                 @"city_id" : self.registerInfo.cityId,
                 @"city_name" : [GetCityManager() fetchCityWithCityId:self.registerInfo.cityId].simpleName,
                 @"company" : self.registerInfo.company,
                 @"address" : self.registerInfo.address,
                 @"contact" : self.registerInfo.contact,
                 @"contact_mobile" : self.registerInfo.contactMobile,
                 @"business_license" : [GetFileManager() generatePreferredPreviewImageURLWithKey:self.registerInfo.businessLicense],
                 @"transportation_license" : [GetFileManager() generatePreferredPreviewImageURLWithKey:self.registerInfo.transportationLicense],
                 @"id_card_front" : [GetFileManager() generatePreferredPreviewImageURLWithKey:self.registerInfo.idCardFront],
                 @"id_card_back" : [GetFileManager() generatePreferredPreviewImageURLWithKey:self.registerInfo.idCardBack],
                 };
    }
}

- (BOOL)canEdit {
    if (self.registerInfo != nil && self.registerInfo.status != UUSupplierReviewStatusApproval && [GetPermissionManager() checkPermission:UUPermissionFlagCompanyReadWrite]) {
        return YES;
    } else {
        return NO;
    }
}

- (void)enterEditVC {
    UUSupplierRegisterVC *registerVC = [UUSupplierRegisterVC instanceOfAppropriate];
    registerVC.previewData = [self previewFields];
    WEAK_SELF();
    [registerVC setCompleteBlock:^(NSDictionary *data) {
        [weakSelf updateSupplierInfoWithData:data];
    }];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)reloadSupplierInfo {
    ShowIndicator();
    WEAK_SELF();
    
    UUUserInfo *userInfo = [GetUserManager() fetchUserInfo];
    [GetSupplierManager() asyncFetchSupplierRegisterInfoWithSupplierId:userInfo.supplierId
                                                         completeBlock:^(UUSupplierRegisterInfo *registerInfo, NSError *error) {
                                                             if (error == nil) {
                                                                 HideIndicatorInView(weakSelf.view);
                                                                 weakSelf.registerInfo = registerInfo;
                                                                 weakSelf.webView.hidden = NO;
                                                                 [weakSelf reloadForm];
                                                             } else {
                                                                 ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                                             }
                                                         }];
}

- (void)updateSupplierInfoWithData:(NSDictionary *)data {
    ShowIndicatorInKeyWindow();
    [GetSupplierManager() asyncUpdateSupplierRegisterInfoWithSupplierId:self.registerInfo.id
                                                                 cityId:data[@"city_id"]
                                                                company:data[@"company"]
                                                                address:data[@"address"]
                                                                contact:data[@"contact"]
                                                          contactMobile:data[@"contact_mobile"]
                                                  transportationLicense:data[@"transportation_license"]
                                                        businessLicense:data[@"business_license"]
                                                            idCardFront:data[@"id_card_front"]
                                                             idCardBack:data[@"id_card_back"]
                                                          completeBlock:^(NSError *error) {
                                                              HideIndicatorInKeyWindow();
                                                              
                                                              if (error == nil) {
                                                                  [self.navigationController popToViewController:self animated:YES];
                                                                  [self reloadSupplierInfo];
                                                              } else {
                                                                  ShowTipsInKeyWindow(error.localizedDescription);
                                                              }
                                                          }];
}

@end

